//
//  LoginUIConfig.swift
//  CMB250716
//
//  Created by Andy Lau on 16/7/25.
//

import SwiftUI

/// 登录界面UI交互配置
struct LoginUIConfig {
    
    // MARK: - 输入框交互配置
    
    /// 输入框响应延迟配置（秒）
    static let textFieldResponseDelay: Double = 0.0
    
    /// 输入框动画持续时间（秒）
    static let textFieldAnimationDuration: Double = 0.2
    
    /// 输入框焦点切换动画持续时间（秒）
    static let focusTransitionDuration: Double = 0.15
    
    // MARK: - 键盘交互配置
    
    /// 键盘自动收起延迟（秒）
    static let keyboardDismissDelay: Double = 0.1
    
    /// 点击空白区域收起键盘
    static let enableTapToDismissKeyboard: Bool = true
    
    /// 切换登录方式时自动收起键盘
    static let dismissKeyboardOnMethodSwitch: Bool = true
    
    /// 键盘收起动画持续时间（秒）
    static let keyboardDismissAnimationDuration: Double = 0.3
    
    // MARK: - 按钮交互配置
    
    /// 按钮点击反馈延迟（秒）
    static let buttonFeedbackDelay: Double = 0.05
    
    /// 按钮状态切换动画持续时间（秒）
    static let buttonStateAnimationDuration: Double = 0.2
    
    /// 第三方登录按钮点击延迟（秒）
    static let thirdPartyLoginDelay: Double = 0.1
    
    // MARK: - 视觉反馈配置
    
    /// 输入框边框高亮动画持续时间（秒）
    static let borderHighlightDuration: Double = 0.25
    
    /// 输入框背景透明度变化动画持续时间（秒）
    static let backgroundOpacityDuration: Double = 0.2
    
    /// 焦点状态下的边框颜色透明度
    static let focusedBorderOpacity: Double = 0.8
    
    /// 非焦点状态下的边框颜色透明度
    static let unfocusedBorderOpacity: Double = 0.5
    
    // MARK: - 性能优化配置
    
    /// 启用输入框性能优化
    static let enableTextFieldOptimization: Bool = true
    
    /// 启用动画性能优化
    static let enableAnimationOptimization: Bool = true
    
    /// 启用触摸反馈优化
    static let enableHapticFeedback: Bool = true
    
    /// 输入框防抖延迟（毫秒）
    static let textFieldDebounceDelay: Int = 100
    
    /// 启用UI响应优化（异步处理非关键操作）
    static let enableUIResponseOptimization: Bool = true
    
    /// 启用首次点击优化（减少初始渲染延迟）
    static let enableFirstTapOptimization: Bool = true
    
    // MARK: - 交互体验配置
    
    /// 启用智能焦点管理
    static let enableSmartFocusManagement: Bool = true
    
    /// 启用手势识别优化
    static let enableGestureOptimization: Bool = true
    
    /// 启用键盘避让优化
    static let enableKeyboardAvoidance: Bool = true
    
    /// 键盘避让动画持续时间（秒）
    static let keyboardAvoidanceDuration: Double = 0.3
    
    // MARK: - 调试配置
    
    /// 启用交互调试模式
    static let enableInteractionDebug: Bool = false
    
    /// 显示焦点状态指示器
    static let showFocusIndicator: Bool = false
    
    /// 显示触摸区域指示器
    static let showTouchAreaIndicator: Bool = false
}

/// 登录界面交互状态管理
class LoginInteractionManager: ObservableObject {
    @Published var isKeyboardVisible: Bool = false
    @Published var currentFocusedField: LoginField? = nil
    @Published var isProcessingInput: Bool = false
    
    private var keyboardDismissTimer: Timer?
    private var inputDebounceTimer: Timer?
    
    enum LoginField: CaseIterable {
        case phoneNumber
        case verificationCode
        case inviteCode
    }
    
    /// 设置当前焦点字段
    func setFocus(to field: LoginField?) {
        withAnimation(.easeInOut(duration: LoginUIConfig.focusTransitionDuration)) {
            currentFocusedField = field
        }
        
        if field != nil {
            isKeyboardVisible = true
        }
    }
    
    /// 收起键盘
    func dismissKeyboard() {
        if LoginUIConfig.enableTapToDismissKeyboard {
            keyboardDismissTimer?.invalidate()
            keyboardDismissTimer = Timer.scheduledTimer(withTimeInterval: LoginUIConfig.keyboardDismissDelay, repeats: false) { _ in
                withAnimation(.easeInOut(duration: LoginUIConfig.keyboardDismissAnimationDuration)) {
                    self.currentFocusedField = nil
                    self.isKeyboardVisible = false
                }
            }
        }
    }
    
    /// 切换登录方式时收起键盘
    func switchLoginMethod() {
        if LoginUIConfig.dismissKeyboardOnMethodSwitch {
            dismissKeyboard()
        }
    }
    
    /// 处理输入防抖
    func handleInputDebounce(action: @escaping () -> Void) {
        inputDebounceTimer?.invalidate()
        inputDebounceTimer = Timer.scheduledTimer(withTimeInterval: Double(LoginUIConfig.textFieldDebounceDelay) / 1000.0, repeats: false) { _ in
            action()
        }
    }
    
    /// 提供触摸反馈
    func provideFeedback() {
        if LoginUIConfig.enableHapticFeedback {
            if LoginUIConfig.enableUIResponseOptimization {
                // 异步处理触觉反馈，避免阻塞UI响应
                DispatchQueue.main.async {
                    let impactFeedback = UIImpactFeedbackGenerator(style: .light)
                    impactFeedback.impactOccurred()
                }
            } else {
                let impactFeedback = UIImpactFeedbackGenerator(style: .light)
                impactFeedback.impactOccurred()
            }
        }
    }
    
    /// 异步提供触摸反馈（专用于首次点击优化）
    func provideFeedbackAsync() {
        if LoginUIConfig.enableHapticFeedback && LoginUIConfig.enableFirstTapOptimization {
            DispatchQueue.main.async {
                let impactFeedback = UIImpactFeedbackGenerator(style: .light)
                impactFeedback.impactOccurred()
            }
        }
    }
    
    deinit {
        keyboardDismissTimer?.invalidate()
        inputDebounceTimer?.invalidate()
    }
}